package Classic150;

public class 最后一个单词的长度 {
    /*
        反向遍历
     */
    public int lengthOfLastWord(String s) {
        char[] charArray = s.toCharArray();
        int index = charArray.length - 1;
        int len = 0;
        while (charArray[index] == ' ') index--;
        while (index >= 0 && charArray[index] != ' ') {
            len++;
            index--;
        }
        return len;
    }
}
